<template>
	<mescroll-uni ref="mescrollRef" @init="mescrollInit" :height="height+'px'" :down="downOption" @down="downCallback"
		:up="upOption" @up="upCallback" @emptyclick="emptyClick" top="0">
		<!-- 金刚区 -->
		<!-- <view class="pt-2">
			<view class="navbox bj_fff">
			    <view class="li" @click="get_link('cases')">
			        <view class="img"><image src="@/static/img/indexnav/nav1.png" mode="aspectFill"></image></view>
			        <view class="name">追剧</view>
			    </view>
			    <view class="li" @click="get_link('custom?type=news')">
			        <view class="img"><image src="@/static/img/indexnav/nav2.png" mode="aspectFill"></image></view>
			        <view class="name">最新</view>
			    </view>
			    <view class="li" @click="get_link('custom')">
			        <view class="img"><image src="@/static/img/indexnav/nav3.png" mode="aspectFill"></image></view>
			        <view class="name">排行</view>
			    </view>
			    <view class="li" @click="get_link('custom?type=free')">
			        <view class="img"><image src="@/static/img/indexnav/nav4.png" mode="aspectFill"></image></view>
			        <view class="name">免费</view>
			    </view>
			</view>
		</view> -->
		
		<view class="qjtitle">
		    <view class="icon"><image src="@/static/img/indexnav/cnxh.png" mode="aspectFill"></image></view>
		    <view class="h2">热门推荐</view>
		</view>
		<view class="flex flex-wrap gap-5 px-4">
			<view v-for="(item,index) in mrtj" :key="index" class="flex flex-col items-center" @click="get_play_url(item.id)">
				<view style="width: 200rpx;height: 280rpx;background-color: #F2F2F2;" class="rounded-t-md">
					<image :src="item.pic" mode="aspectFill" style="width: 200rpx;height: 280rpx;" class="rounded-t-md"></image>
				</view>
				<view class="bg-white rounded-b-md">
					<view class="truncate text-title text-center p-1 box-border" style="width:200rpx;">{{item.name}}</view>
					<view class="truncate text-sub text-gray-400 text-center" style="width:200rpx;">{{computedNav(item.cid)}} {{item.state}}</view>
				</view>
			</view>
		</view>
		<view class="qjtitle">
		    <view class="icon"><image src="@/static/img/indexnav/nav3.png" mode="aspectFill"></image></view>
		    <view class="h2">排行榜</view>
		</view>
		<view class="flex flex-wrap gap-5 px-4">
			<view v-for="(item,index) in phb" :key="index" class="flex flex-col items-center" @click="get_play_url(item.id)">
				<view style="width: 200rpx;height: 280rpx;background-color: #F2F2F2;" class="rounded-t-xl">
					<image :src="item.pic" mode="aspectFill" style="width: 200rpx;height: 280rpx;" class="rounded-t-xl"></image>
				</view>
				<view class="bg-white rounded-b-md">
					<view class="truncate text-title text-center p-1 box-border" style="width:200rpx;">{{item.name}}</view>
					<view class="truncate text-sub text-gray-400 text-center" style="width:200rpx;">{{computedNav(item.cid)}} {{item.state}}</view>
				</view>
			</view>
		</view>
		<view class="qjtitle">
		    <view class="icon"><image src="@/static/img/indexnav/nav1.png" mode="aspectFill"></image></view>
		    <view class="h2">最新更新</view>
		</view>
		<view class="flex flex-wrap gap-5 px-4">
			<view v-for="(item,index) in goods" :key="index" class="flex flex-col items-center" @click="get_play_url(item.id)">
				<view style="width: 200rpx;height: 280rpx;background-color: #F2F2F2;" class="rounded-t-xl">
					<image :src="item.pic" mode="aspectFill" style="width: 200rpx;height: 280rpx;" class="rounded-t-xl"></image>
				</view>
				<view class="bg-white rounded-b-md">
					<view class="truncate text-title text-center p-1 box-border" style="width:200rpx;">{{item.name}}</view>
					<view class="truncate text-sub text-gray-400 text-center" style="width:200rpx;">{{computedNav(item.cid)}} {{item.state}}</view>
				</view>
			</view>
		</view>
	</mescroll-uni>
</template>

<script>
	let that = null;
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		computed:{
			// 返回分类类型
			computedNav(){
				return (cid)=>{
					let navList = uni.getStorageSync('nav-list');
					let obj = navList.find(item=>item.id == cid)
					return obj.name
				}
			}
			
		},
		props: {
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			},
			tabs: {
				type: Array,
				default () {
					return []
				}
			},
			height:{
				type: [Number,String],
				default () {
					return ''
				}
			}
		},
		data() {
			return {
				downOption: {
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					noMoreSize: 4, //如果列	表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty: {
						tip: '~ 暂无记录 ~',
						fixed:true,
						top: "200rpx",
					},
					textNoMore:'-- 触底了 --'
				},
				mrtj:[],// 热门推荐
				phb:[],// 排行榜
				goods: [] ,//列表数据
			}
		},
		mounted() {
			that = this;
			// 获取首页推荐
			this.getHome()
		},
		methods: {
			// 点击播放跳转
			get_play_url(id){
				let url = 'play?id='+id;
				// #ifdef APP-PLUS
					url = 'aplay?id='+id;
				// #endif
				that.get_link(url);
			},
			// 点击跳转
			get_link(url){
				if(url == 'cases'){
					uni.switchTab({
						url: '/pages/'+url
					})
					return;
				}
				uni.navigateTo({
					url: '/pages/'+url
				});
			},
			/*下拉刷新的回调 */
			downCallback() {
				this.goods = []
				this.mescroll.resetUpScroll()
				// 获取首页推荐
				this.getHome()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				//联网加载数据
				this.getList(page)
			},
			// 获取最近更新数据
			getList(page){
				let req = {
					cid:this.tabs[this.index].id=='-1'?'':this.tabs[this.index].id,
					size: page.size, //每页的数量 
					page: page.num, // 当前显示第几页
					type:'news',// 类型 最近更新news  排行榜hot 推荐reco
				} 
				uni.$u.http.get('/index/data', {params:req}).then(res => {
					if(res.code==1&&res.data&&res.data.list){
						this.goods=this.goods.concat(res.data.list)
						this.mescroll.endSuccess(res.data.list.length,res.data.pageload);
					}else{
						this.mescroll.endSuccess(0,false);
						this.goods=[]
					}
				}).catch((err)=>{
					//联网失败, 结束加载
					this.mescroll.endErr();
				})
			},
			// 获取首页推荐
			getHome(){
				let req = {
					cid:this.tabs[this.index].id=='-1'?'':this.tabs[this.index].id
				} 
				uni.$u.http.get('/index/home', {params:req}).then(res => {
					if (res.code == 1&&res.data) {
						this.mrtj=res.data.reco
						this.phb=res.data.hot
						
						// this.goods=res.data.news
					}
				})
			},
			//点击空布局按钮的回调
			emptyClick() {
				uni.showToast({
					title: '点击了按钮',
					icon:"none"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.text-title{
		font-size: 24rpx;
	}
	.text-sub{
		font-size: 22rpx;
	}
/* 金刚区 */
.navbox{width: calc(100% - 32px);margin: 0 auto;padding-top: 6px;box-shadow: 0px 2px 16px 0px rgba(0,0,0,.04);border-radius: 5px;display: flex;justify-content: space-between;}
.navbox .li{width: 25%;height: 75px;display: inline-block;cursor: pointer;}
.navbox .li .img{width: 50px;height: 50px;margin: 0 auto;}
.navbox .li .img image{width: 100%;height: 100%;object-fit: cover;}
.navbox .li .name{font-size: 14px;font-weight: 600;text-align: center;}
/* 全局qjtitle */
.qjtitle{width: 100%;height: 30px;position: relative; padding: 10px;}
.qjtitle .icon{width: 20px;height: 20px;display: inline-block;vertical-align: middle;margin-right: 10px;}
.qjtitle .icon image{width: 100%;height: 100%;object-fit: cover;}
.qjtitle .h2{font-size: 16px;font-weight: 600;display: inline-block;vertical-align: middle;line-height: 30px;}
.qjtitle .sub{font-size: 14px;color: #999;line-height: 30px;position: absolute;top: 0;right: 0;}
</style>